﻿using biaoqian;
using Kingdee.BOS.WebApi.Client;
using Kingdee.BOS.WebApi.DataEntify;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Wkol.Models;

namespace Wkol.Controllers 
{
    public class Attachment_matchingController : Controller
    {
        Class1 one = new Class1();
        Delete_file_name_suffix DFNS = new Delete_file_name_suffix();
        LDhuoyunEntities db = new LDhuoyunEntities();
        // GET: Attachment_matching
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Tables_reports(string combs,int state = 0,string path="")
        {
            if (state == 1)
            {
                
                DateTime now = DateTime.Now;
                string currentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");

                K3CloudApi client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "SAL_DELIVERYNOTICE";
                //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FBillNo,F_SMGS_BaseProperty7,FReceiveAddress,F_SMGS_KM,F_SMGS_Integer4,FMaterialName,F_SMGS_YFKG,F_SMGS_Decimal1";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var ZBLx = combs;
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FBillNo like'%" + ZBLx + "%'and FBillTypeID ='193822715afc48aa9fa6d6beca7700ab'and FPlanDeliveryDate >'" + currentDateTime + "'and FCancelStatus='A'",
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 120,
                    OrderString = "FBillNo",
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());

                DataTable dataTable = one.Receipt();
                path = "D:\\回单测试文件夹";
                if (Directory.Exists(path))
                {
                    // 遍历指定目录下的所有文件  
                    string[] files = Directory.GetFiles(path);

                    // 将文件名添加到列表中  
                    
                    foreach (var item in result)
                    {
                        string File_path = "";
                        foreach (string file in files)
                        {

                            FileInfo fileInfo = new FileInfo(file);
                            string input = fileInfo.Name;
                            char delimiter = '.';
                            string temp = DFNS.RemoveAfterChar(input, delimiter);

                            if (item[0].ToString() == temp)
                            {
                                File_path = fileInfo.FullName;
                            }
                        }
                        dataTable.Rows.Add(item[0], item[1], item[2], File_path);
                    }
                }
                else
                {
                    throw new DirectoryNotFoundException("The specified directory does not exist.");
                }
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(dataTable, settings);
                return Content(jsongStr, "application/json");
            }
            else if (state == 2)
            {
                DateTime now = DateTime.Now;
                string currentDateTime = now.ToString("yyyy-MM-dd HH:mm:ss");
                K3CloudApi client = new K3CloudApi();
                //用于记录结果
                StringBuilder Info = new StringBuilder();
                //业务对象标识
                string formId = "SAL_DELIVERYNOTICE";
                //查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FBillNo,F_SMGS_BaseProperty7,FReceiveAddress,F_SMGS_KM,F_SMGS_Integer4,FMaterialName,F_SMGS_YFKG,F_SMGS_Decimal1";
                //请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var ZBLx = combs;
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FBillNo like'%" + ZBLx + "%'and FBillTypeID ='193822715afc48aa9fa6d6beca7700ab'and FPlanDeliveryDate >'" + currentDateTime + "'and FCancelStatus='A'",
                    TopRowCount = 0,
                    StartRow = 0,
                    Limit = 120,
                    OrderString = "FBillNo",
                };
                //调用接口
                var result = client.ExecuteBillQuery(param.ToJson());
                //转换成标准文档并遍历
                DataTable dataTable = one.Receipt_BH();
                foreach (var item in result)
                {
                    dataTable.Rows.Add(item[0], item[1]);
                }
                JsonSerializerSettings settings = new JsonSerializerSettings();
                string jsongStr = JsonConvert.SerializeObject(dataTable, settings);
                return Content(jsongStr, "application/json");
            }
            return View();
        }



        public ActionResult add_tbjl(List<TBJL> tbjl)
        {
            int sun=0;
            if (tbjl == null)
            {
                tbjl = new List<TBJL>();
            }
            foreach (TBJL b in tbjl)
            {
                int count = db.TBJL.Where(x => x.hd_fhdh == b.hd_fhdh).Count();
                if (count == 0)
                {
                    db.TBJL.Add(b);
                    sun += db.SaveChanges();
                }
            }
            return Json(sun);
        }
    }

    
}